package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "用户模块")
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    @PostMapping
    @ApiOperation(value = "新增用户信息")
    public void saveUser(@RequestBody UserFormDTO userFormDTO) {
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户信息")
    public void deleteUser(@PathVariable("id") Long id) {
        userService.removeById(id);
    }
    @ApiOperation(value = "根据id查询用户信息")
    @GetMapping("/{id}")
    public UserVO getUser(@ApiParam("用户id") @PathVariable("id") Long id) {
//        User user = userService.getById(id);
//        return BeanUtil.copyProperties(user, UserVO.class);
        return userService.getUserVOById(id);
    }
    @ApiOperation(value = "根据id查询批量用户信息")
    @GetMapping
    public List<UserVO> getUser(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids) {
        return userService.getUserVOByIds(ids);
    }
    @ApiOperation(value = "扣减用户余额")
    @PutMapping("/{id}/deduct/{money}")
    public void getUser(@ApiParam("用户id") @PathVariable("id") Long id,
                                @ApiParam("扣减金额") @PathVariable("money") Integer money) {
        userService.deductBalance(id, money);
    }
    @ApiOperation(value = "根据复杂条件查询用户信息")
    @GetMapping("/query")
    public List<UserVO> queryUsers( UserQuery userQuery) {
        List<User> users = userService.queryUsers(userQuery);
        return BeanUtil.copyToList(users, UserVO.class);
    }
}

